import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/Login","/login/dgut"})
public class two_ extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doPost(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            ObjectMapper objectMapper = new ObjectMapper();

            HttpPost httpPost = new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken");
            List<NameValuePair> list_data = new ArrayList<>();

            list_data.add(new BasicNameValuePair("token", req.getParameter("token")));
            list_data.add(new BasicNameValuePair("appid", "javaee"));
            list_data.add(new BasicNameValuePair("appsecret", "b3b52e43ccfd"));
            list_data.add(new BasicNameValuePair("userip", req.getLocalAddr()));
            httpPost.setEntity(new UrlEncodedFormEntity(list_data));

            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                HttpEntity entity = response.getEntity();
                JsonNode jsonNode = objectMapper.readTree(EntityUtils.toString(entity, "UTF-8"));

                /*if(entity!=null){
                    System.out.println(entity.toString());
                   // LJD ljd = json.fromJson(EntityUtils.toString(entity,"UTF-8"),LJD.class);
                   // System.out.println("LJD"+ljd.toString()+"\n");
                }*/

                list_data.removeAll(list_data);

                if(Integer.parseInt(jsonNode.get("error").toString()) == 0){
                    httpPost = new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo");
                    list_data.add(new BasicNameValuePair("openid", jsonNode.get("openid").asText()));
                    list_data.add(new BasicNameValuePair("access_token", jsonNode.get("access_token").asText()));
                    httpPost.setEntity(new UrlEncodedFormEntity(list_data));

                    try (CloseableHttpResponse response2 = httpClient.execute(httpPost)) {
                        entity = response2.getEntity();
                        String json = EntityUtils.toString(entity, "UTF-8");
                        JsonNode jsonNode2 = objectMapper.readTree(json);

                        if (jsonNode2.get("error") == null) {
                            
                            LJD ljd = objectMapper.readValue(json, LJD.class);
                            req.getSession().setAttribute("ljd", ljd);
                            req.getSession().setAttribute("token", req.getParameter("token"));
                            resp.sendRedirect("http://localhost:8080/Interface2.jsp");
                        } else
                            resp.getWriter().println(jsonNode2.get("message"));
                    }
                }
                else
                    resp.getWriter().println(jsonNode.get("message"));
                EntityUtils.consume(entity);
            }
        }
    }
}
